package demo1;

import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2024-03-13
 * Time: 9:30
 */
public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A  = new TreeNode('A');
        TreeNode B  = new TreeNode('B');
        TreeNode C  = new TreeNode('C');
        TreeNode D  = new TreeNode('D');
        TreeNode E  = new TreeNode('E');
        TreeNode F  = new TreeNode('F');
        TreeNode G  = new TreeNode('G');
        TreeNode H  = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null)
            return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val+ " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) return;
        inOrder(root.left);
        inOrder(root.right);
        System.out.print(root.val+ " ");

    }
    //获取树中的节点个数
    public static int usedSize = 0;
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        usedSize++;
        size(root.left);
        size(root.right);
        return usedSize;
    }
    public int size2(TreeNode root) {
        if (root ==null){
            return 0;
        }
        return size2(root.left)+size2(root.right)+1;
    }

    //获取叶子结点的个数
    public static int leafSize = 0;
    public int getLeafNodeCount(TreeNode root) {
        if (root ==null) {
            return 0;
        }
        if (root.left ==null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }
    //
    public int getHeight(TreeNode root) {
        if (root == null){
            return 0;
        }
        int leftH= getHeight(root.left);
        int rightH= getHeight(root.right);

        return (leftH > rightH ? leftH : rightH) + 1;
    }

    public TreeNode find(TreeNode root, int val) {
        if (root == null)return null;
        if (root.val == val) {
            return root;
        }
        TreeNode leftL = find(root.left,val);
        if (leftL != null){
            return leftL;
        }
        TreeNode leftR = find(root.right,val);
        if (leftR != null){
            return leftR;
        }
        return null;
    }
    public TreeNode inverTree(TreeNode root) {
        if (root == null) return null;

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        inverTree(root.left);
        inverTree(root.right);
        return root;
    }
    public boolean isSymmetric(TreeNode root) {
        if (root == null) return true;
        return isSymmetricChild(root.left, root.right);
    }
    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree){
        if (leftTree == null && rightTree != null || leftTree != null && rightTree == null){
            return false;
        }
        if (leftTree == null && rightTree == null)
        return true;
        if (leftTree.val != rightTree.val)
        {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right)&& isSymmetricChild(leftTree.right,rightTree.left);
    }
}